/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.projecao.spe.modelo.controle;

import br.projecao.spe.modelo.excecao.SpeException;
import javax.persistence.Entity;
import javax.persistence.EntityManager;

/**
 *
 * @author Desenvolvimento
 */
public class SpeDAO {

    public void incluir(EntityManager em, Entity entity) {
        try {
            em.getTransaction().begin();
            em.persist(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            new SpeException(e.getMessage());
            em.getTransaction().rollback();
        }

    }

    public void excluir(EntityManager em, Entity entity) {
        try {
            em.getTransaction().begin();
            em.remove(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
            new SpeException(e.getMessage());
        }

    }

    public void alterar(EntityManager em, Entity entity) {
        try {
            em.getTransaction().begin();
            em.merge(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
            new SpeException(e.getMessage());
        }
    }
}
